<template>
  <div id="personalinformation">
    <el-tabs tab-position="left" style="height: 100%">
      <el-tab-pane label="登录时间">
        <div class="block" style="margin-left: 2%">
          <el-timeline>
            <el-timeline-item
              v-for="x in v"
              :key="x.id"
              :timestamp="x.loginDate"
              placement="top"
            >
              <div>
                <!-- <p>
                                    <Icon type="ios-pin-outline" style="margin-right: 3px;" />地区： 中国|0|辽宁|大连|联通
                                </p> -->
                <p style="margin-top: 1%">
                  <Icon
                    type="ios-help-buoy-outline"
                    style="margin-right: 3px"
                  />浏览器：{{ x.browser }}
                </p>
                <p style="margin-top: 1%">
                  <Icon
                    type="md-locate"
                    style="margin-right: 3px"
                  />操作系统：{{ x.operatingSystem }}
                </p>
              </div>
            </el-timeline-item>
          </el-timeline>
        </div>
      </el-tab-pane>

      <el-tab-pane label="账号信息">
        <el-form :model="user" style="margin-left: 2%">
          <el-form-item label="系统头像">
            <el-image
              style="
                width: 100px;
                height: 100px;
                border-radius: 50%;
                margin-left: 36%;
              "
              :src="user.avatar"
            ></el-image>
            <Upload
              action="/api/auth/pd_auth_user/uploadcontroller"
              style="margin-left: 41%"
              name="myfile"
              :on-success="successfile"
              :show-upload-list="false"
            >
              <Button icon="ios-cloud-upload-outline">更新头像</Button>
            </Upload>
          </el-form-item>
          <el-form-item label="姓名：" label-width="100px">
            <el-col :span="20">
              <el-input v-model="user.name" placeholder=""></el-input
            ></el-col>
          </el-form-item>
          <el-form-item label="邮箱：" label-width="100px">
            <el-col :span="20">
              <el-input v-model="user.email" placeholder=""></el-input
            ></el-col>
          </el-form-item>
          <el-form-item label="电话：" label-width="100px">
            <el-col :span="20">
              <el-input v-model="user.mobile" placeholder=""></el-input
            ></el-col>
          </el-form-item>
          <el-form-item label="性别：" label-width="100px">
            <el-col :span="20">
              <el-select v-model="user.sex" style="width: 100%">
                <el-option label="男" value="M"></el-option>
                <el-option label="女" value="W"></el-option>
                <el-option label="未知" value="N"></el-option>
              </el-select>
            </el-col>
          </el-form-item>
          <el-form-item label="个人描述：" label-width="100px">
            <el-col :span="20">
              <el-input
                type="textarea"
                :rows="2"
                placeholder="请输入内容"
                v-model="user.workDescribe"
              >
              </el-input>
            </el-col>
          </el-form-item>
        </el-form>
        <Button
          type="error"
          size="large"
          style="width: 100px; margin-left: 11%"
          @click="updateuser"
        >
          修改</Button
        >
      </el-tab-pane>

      <el-tab-pane label="个人密码">
        <el-form
          :model="password"
          status-icon
          :rules="rules"
          style="margin-left: 2%"
          ref="password"
        >
          <el-form-item label="旧密码：" label-width="100px" prop="oldpassword">
            <el-col :span="20">
              <el-input v-model="password.oldpassword" placeholder=""></el-input
            ></el-col>
          </el-form-item>
          <el-form-item label="新密码：" label-width="100px" prop="newpassword">
            <el-col :span="20">
              <el-input v-model="password.newpassword" placeholder=""></el-input
            ></el-col>
          </el-form-item>
          <el-form-item
            label="确定密码："
            label-width="100px"
            prop="newnewpasword"
          >
            <el-col :span="20">
              <el-input
                v-model="password.newnewpasword"
                placeholder=""
              ></el-input
            ></el-col>
          </el-form-item>
          <Button
            type="error"
            size="large"
            style="width: 100px; margin-left: 11%"
            @click="updatepassword('password')"
            >修改</Button
          >
        </el-form>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
export default {
  data() {
    var validatePassxxxx = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.password.newpassword) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      user: {},
      password: {},
      v: [],
      rules: {
        oldpassword: [
          { required: true, message: "请输入旧密码", trigger: "blur" },
          { validator: validatePassxxxx, trigger: "blur" },
        ],
        newpassword: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { validator: validatePass, trigger: "blur" },
        ],
        newnewpasword: [
          { required: true, message: "请再次输入密码", trigger: "blur" },
          { validator: validatePass2, trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    //查询用户编号查询登录记录
    loginhistory() {
      const loading = this.$loading({
        lock: true,
        text: "加载中",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      var x = JSON.parse(sessionStorage.getItem("user"));
      this.axios
        .get("/api/auth/pd_common_login_log/queryloginhistory/" + x.id)
        .then((res) => {
          loading.close();
          this.v = res.data.data;
        })
        .catch((err) => {
          loading.close();
          console.error(err);
        });
    },
    //上传成功回调
    successfile(response, file, fileList) {
      this.user.avatar = response;
    },
    updateuser() {
      //修改用户
      // console.log(this.user)
      const loading = this.$loading({
        lock: true,
        text: "加载中",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      this.axios
        .post("/api/auth/pd_auth_user/updateuser", this.user)
        .then((res) => {
          loading.close();
          this.$message({
            message: "账号修改成功",
            type: "success",
          });
          sessionStorage.setItem("user", JSON.stringify(this.user));
          this.$router.push("/index");
          location.reload();
        })
        .catch((err) => {
          loading.close();
          console.error(err);
        });
    },
    updatepassword(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.password.id = this.user.id;
          const loading = this.$loading({
            lock: true,
            text: "加载中",
            spinner: "el-icon-loading",
            background: "rgba(0, 0, 0, 0.7)",
          });
          this.axios
            .post("/api/auth/pd_auth_user/updatepassword", this.password)
            .then((res) => {
              if (res.data.code == 200) {
                this.$Message.success(res.data.message);
              } else {
                this.$Message.error(res.data.message);
              }
              loading.close();
              this.password = {};
              this.$router.push("/index");
            })
            .catch((err) => {
              loading.close();
              console.error(err);
            });
        } else {
          loading.close();
          this.$Message.error("表单错误请检查后提交！！！");
          return false;
        }
      });
    },
  },
  created() {
    this.loginhistory();
    this.user = JSON.parse(sessionStorage.getItem("user"));
  },
};
</script>

<style scoped></style>
